﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using data_adapter;

namespace pallete_main
{
    public class DeviceTree : TreeView
    {
        private DataSet dataset;
        private Hashtable htParentNode;

        public DeviceTree()
        {
            this.Dock = DockStyle.Fill;
            this.CheckBoxes = true;
        }

        public void createDeviceTree(string schema) 
        {
            htParentNode = new Hashtable();
            dataset = new DataSet();
            string sql = "select d.device_id,d.name,d.device_type,d.description,b.bay_id,b.name,c.cubicle_id,c.name,d.vlevel from "
                         + schema + ".device d," + schema + ".cubicle c," + schema + ".bay b where d.device_type!=2 and d.cubicle_id=c.cubicle_id and b.bay_id=d.bay_id";

           
            DataAdapter adapter = new DataAdapter();
            adapter.InitConnection();
            dataset = adapter.GetDataSet(sql);
            //adapter.CloseConnection();

        }

        public void changeShowType(_device_show_type type)
        {
            htParentNode.Clear();
            this.Nodes.Clear();
            DataTable datatable = new DataTable();
            DataView dataview = dataset.Tables[0].DefaultView;
            switch (type)
            {
                case _device_show_type.BAY:
                    dataview.Sort = "bay_id asc,device_type asc,name asc";
                    break;
                case _device_show_type.LEVEL:
                    dataview.Sort = "vlevel asc,device_type asc,name asc";
                    break;
                case _device_show_type.CUBICLE:
                    dataview.Sort = "cubicle_id asc,device_type asc,name asc";
                    break;
                case _device_show_type.DEVICE:
                    dataview.Sort = "name asc";
                    break;
            };

            datatable = dataview.ToTable();
            //ds1.Tables[0].DefaultView.ToTable();

            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                Console.WriteLine("device_id=" + datatable.Rows[i][0].ToString());
                int device_id = Convert.ToInt32(datatable.Rows[i][0]);
                string parentName = null;
                string nodeName = (string)datatable.Rows[i][1];
                if ( Convert.ToInt32(datatable.Rows[i][2]) == 0) {
                    nodeName = nodeName + ":" + (string)datatable.Rows[i][3];
                }
                switch (type)
                {
                    case _device_show_type.BAY:
                        parentName = (string)datatable.Rows[i][5];
                        //nodeName = (string)datatable.Rows[i][1];
                        break;
                    case _device_show_type.LEVEL:
                        parentName = ((int)datatable.Rows[i][8]).ToString()+"kV";
                        //nodeName = (string)datatable.Rows[i][1];
                        break;
                    case _device_show_type.CUBICLE:
                        parentName = ((int)datatable.Rows[i][7]).ToString();
                        //nodeName = (string)datatable.Rows[i][1];
                        break;
                    case _device_show_type.DEVICE:
                        parentName = (string)datatable.Rows[i][1];
                        break;
                };

                if (!htParentNode.ContainsKey(parentName))
                {
                    DeviceTreeNode parentNode = new DeviceTreeNode(null, parentName);
                    htParentNode.Add(parentName, parentNode);
                    this.Nodes.Add(parentNode);
                   // Console.WriteLine("node name=" + parentName);
                }

                DeviceTreeNode node = new DeviceTreeNode(device_id, nodeName);
                //htNode.Add(childName, node);
                ((TreeNode)htParentNode[parentName]).Nodes.Add(node);

            }
            //this.Refresh();

        }

        public int[] getSelectedDeviceId() {
           List<int> deviceList = new List<int>();
           for(int i=0;i<this.Nodes.Count;i++){
             DeviceTreeNode treeNode = (DeviceTreeNode)this.Nodes[i];
             if(treeNode.Checked && treeNode.getDeviceId() != null){
                 deviceList.Add((int)treeNode.getDeviceId());
             }else if(treeNode.getDeviceId() != null){
               continue;
             }else{
                for(int j=0;j<treeNode.Nodes.Count;j++){
                   if(treeNode.Nodes[j].Checked){
                      deviceList.Add((int)((DeviceTreeNode)treeNode.Nodes[j]).getDeviceId());
                   }
                }
             }
             
           }
           return deviceList.ToArray();
        }

    }

    public enum _device_show_type
    {
        BAY = 0,      //按间隔
        LEVEL = 1,     //按电压
        CUBICLE = 2,     //按屏柜
        DEVICE = 3,    //按设备
    };

    public class DeviceTreeNode : TreeNode
    {
        private int? deviceId;
        public DeviceTreeNode(int? deviceId, string name)
            : base(name)
        {
            this.deviceId = deviceId;

        }

        public int? getDeviceId()
        {
            return deviceId;
        }
    }


}
